package xiangmu.schoolpaper.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xiangmu.schoolpaper.config.JwtProperties;
import xiangmu.schoolpaper.constant.JwtClaimsConstant;
import xiangmu.schoolpaper.context.BaseContext;
import xiangmu.schoolpaper.exception.LoginFailedException;
import xiangmu.schoolpaper.pojo.DTO.LoginDTO;
import xiangmu.schoolpaper.pojo.VO.LoginVO;
import xiangmu.schoolpaper.pojo.entity.User;
import xiangmu.schoolpaper.result.Result;
import xiangmu.schoolpaper.service.LoginService;
import xiangmu.schoolpaper.utils.JwtUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
@Slf4j
@Tag(name="登录接口")

public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @Operation(summary = "1登录")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO){
        log.info("开始进行登录");
        User user = loginService.login(loginDTO);

        //判断登录时账户和密码是否为空
        if (loginDTO.getAccount()==null|| loginDTO.getPassword()==null){
            throw  new LoginFailedException("用户名或密码不能为空");
        }

        //判断登录成功
        if(user!=null){
            LoginVO loginVO =new LoginVO();
            loginVO.setId(user.getId());
            loginVO.setUsername(user.getAccount());
            loginVO.setName(user.getName());
            loginVO.setRoleId(user.getRoleId());
            Map<String, Object> claims=new HashMap<>();
            claims.put(JwtClaimsConstant.USER_ID, user.getId());
            claims.put(JwtClaimsConstant.NAME, user.getName());
            claims.put(JwtClaimsConstant.USERNAME, user.getAccount());
            claims.put(JwtClaimsConstant.ROLE_ID, user.getRoleId());
            String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);
            loginVO.setToken(token);
            return Result.success(loginVO);
        }
        return Result.error("登录失败，请检查用户名和密码是否正确");
    }

//    根据token返回角色ID，以实现路由跳转
//    根据前端请求头token解析出roleId返回给前端
    @GetMapping("/login/get-role-id")
    @Operation(summary = "2获取登录用户的角色")
    public Result<Long> getRoleId(){
        Long roleId =  (Long) BaseContext.get("roleId");
        return Result.success(roleId);
    }

//    根据token返回角色ID，以实现路由跳转
//    根据前端请求头token解析出roleId返回给前端
    @GetMapping("/login/get-user-id")
    @Operation(summary = "3获取登录用户的id")
    public Result<Long> getUserId(){
        Long userId =  (Long) BaseContext.get("userId");
        return Result.success(userId);
    }

//    根据token返回用户账号(account)，以完成个人中心的用户信息查看功能
//    根据前端请求头token解析出的username(JwtClaimsConstant)返回给前端
//    前端再根据获取到的account来获取用户信息（调用另外一个接口）
    @GetMapping("/login/get-account")
    @Operation(summary = "4获取登录用户的账号")
    public Result<String> getAccount() {
        String account = (String) BaseContext.get("account");
        return Result.success(account);
    }
}
